$(function() {
	var maintainpage = 1;
	var refuelpage = 1;
	var insurepage = 1;
	var surveypage = 1;
	 laydate.render({
	  elem: '.time'
	  ,range: true,
	  done:function(value, date, endDate){
	  	arr = value.split(' - ')
	  	$("input[name='order_start']").val(arr[0])
	  	$("input[name='order_end']").val(arr[1])
	  }
	});
	var maintaintddata = [{
		key: 'start_date',
		type: 2,
		url: '{:url("order/detail")}',
		url: ''
	}, {
		key: 'leader',
		type: 1,

		id: 137
	}, {
		key: 'address',
		type: 1,
		url: ''
	}, {
		key: 'price',
		type: 1,
		url: ''
	}, {
		key: 'service_type2',
		type: 1,
		url: ''
	}, {
		key: 'state2',
		type: 1,
		url: ''
	}];
	var refueltddata = [{
		key: 'start_date',
		type: 2,
		url: '{:url("order/detail")}',
		url: ''
	}, {
		key: 'driver',
		type: 1,

		id: 137
	}, {
		key: 'refuel_add',
		type: 1,
		url: ''
	}, {
		key: 'price',
		type: 1,
		url: ''
	}, {
		key: 'label',
		type: 1,
		url: ''
	}];
	var insuretddata = [{
		key: 'number',
		type: 2,
		url: '{:url("order/detail")}',
		url: ''
	}, {
		key: 'policy_number',
		type: 1,

		id: 137
	}, {
		key: 'policy_holder',
		type: 1,
		url: ''
	}, {
		key: 'policy_type',
		type: 1,
		url: ''
	}, {
		key: 'insurance_company',
		type: 1,
		url: ''
	}, {
		key: 'policy_charge',
		type: 1,
		url: ''
	}, {
		key: 'start_date',
		type: 1,
		url: ''
	}, {
		key: 'end_date',
		type: 1,
		url: ''
	}];
	var surveytddata = [{
		key: 'mot_record_id',
		type: 2,
		url: '{:url("order/detail")}',
		url: ''
	}, {
		key: 'number',
		type: 1,

		id: 137
	}, {
		key: 'start_date',
		type: 1,
		url: ''
	}, {
		key: 'mot_add',
		type: 1,
		url: ''
	}, {
		key: 'checker',
		type: 1,
		url: ''
	}, {
		key: 'state',
		type: 1,
		url: ''
	}, {
		key: 'price',
		type: 1,
		url: ''
	}];
	var surveyaddoperation = `<td>
			<div class="tpl-table-black-operation">
				<a class="exit" href="javascript:;">
					<i class="am-icon-pencil"></i> 编辑
				</a>
				<a class="tpl-table-black-operation-del delete-list" data-method="delete">
					<i class="am-icon-pencil"></i> 删除
				</a>
			</div>
		</td>`;
	var maintainaddoperation = `<td>
			<div class="tpl-table-black-operation">
				<a class="exit" class="" href="javascript:;">
					<i class="am-icon-pencil"></i> 编辑
				</a>
				<a class="tpl-table-black-operation-del delete-list" data-method="delete">
					<i class="am-icon-pencil"></i> 删除
				</a>
			</div>
		</td>`;

	var insureaddoperation = `<td>
			<div class="tpl-table-black-operation">
				<a class="exit" class="" href="javascript:;">
					<i class="am-icon-pencil"></i> 编辑
				</a>
				<a class="tpl-table-black-operation-del delete-list" data-method="delete">
					<i class="am-icon-pencil"></i> 删除
				</a>
			</div>
		</td>`;
	var refueladdoperation = `<td>
			<div class="tpl-table-black-operation">
				<a class="exit" class="" href="javascript:;">
					<i class="am-icon-pencil"></i> 编辑
				</a>
				<a class="tpl-table-black-operation-del delete-list" data-method="delete">
					<i class="am-icon-pencil"></i> 删除
				</a>
			</div>
		</td>`;
		{if $Think.get.type==1}
			$('.cost').tabs('open', 0)
			var datamaintain={};
			var maintainwhere_json={};
			maintainwhere_json.company_id=['in',$('.company_all_id').val()] 
			datamaintain.where_json=JSON.stringify(maintainwhere_json)
			datamaintain.pagenum=10;
			maintain(datamaintain)
		{elseif $Think.get.type==2/}
		$('.cost').tabs('open', 1)
			refuel(maintainpage)
		{elseif $Think.get.type==3/}
		$('.cost').tabs('open', 2)
			insure(maintainpage)
		{elseif $Think.get.type==4/}
		$('.cost').tabs('open', 3)
			survey(maintainpage)
		{else/}
		$('.cost').tabs('open', 0)
		var datamaintain={};
		var maintainwhere_json={};
		maintainwhere_json.company_id=['in',$('.company_all_id').val()] 
		datamaintain.where_json=JSON.stringify(maintainwhere_json)
		datamaintain.pagenum=10;
			maintain(datamaintain)
		{/if}
	

	$('.tab_maintain').on('click', function() {
		var datamaintain={};
		var maintainwhere_json={};
		maintainwhere_json.company_id=['in',$('.company_all_id').val()] 
		datamaintain.where_json=JSON.stringify(maintainwhere_json)
		datamaintain.pagenum=10;
		
		maintain(datamaintain);
	})
	$('.tab_refuel').on('click', function() {
		refuel();
	})
	$('.tab_insure').on('click', function() {
		insure();
	})
	$('.tab_survey').on('click', function() {
		survey();
	})
	$('.maintain .search').on('click',function(){
		var data={};
		var where_json={};
		where_json.plate_no=['like','%'+$('.maintain').find('input[name="number"]').val()+'%'] ;
		where_json.company_id=['in',$('.company_all_id').val()] 
		where_json.start_date=['between time',[$('.order_start').val(),$('.order_end').val()]]
		data.where_json=JSON.stringify(where_json)
		data.pagenum=10;
		
	
		console.log(data)
		maintain(data)
	})
	$('.refuel .search').on('click',function(){
		var data={
			"c-number":$('.refuel').find('input[name="number"]').val(),
			start_date:$('.refuel').find('input[name="start_date"]').val(),
			end_date:$('.refuel').find('input[name="end_date"]').val()
		};
		
		console.log(data)
		refuel(data)
	})
	$('.survey .search').on('click',function(){
		var data={
			"c-number":$('.survey').find('input[name="number"]').val(),
			start_date:$('.survey').find('input[name="start_date"]').val(),
			end_date:$('.survey').find('input[name="end_date"]').val()
		};
		
		console.log(data)
		survey(data)
	})
	$('.insure .search').on('click',function(){
		var data={
			"c-number":$('.insure').find('input[name="number"]').val(),
			start_date:$('.insure').find('input[name="start_date"]').val(),
			end_date:$('.insure').find('input[name="end_date"]').val()
		};
		
		console.log(data)
		insure(data)
	})
	function maintain(data) {
		Load()
		if(data){
		data.page=maintainpage
		}
		console.log(data)
		$.ajax({
			type: "post",
			url: "{:url('adminapi/Maintenancerecord/recordList')}",
			data: data,
			success: function(res) {
				Load(1)
				console.log(res)
				if(res.code == 1) {
					$("#loading-first").fadeOut(500);
					var maintainli = res.list
					for(var mainidx=0;mainidx<maintainli.length;mainidx++){
						console.log(maintainli[mainidx].service_type)
						if(maintainli[mainidx].service_type==1){
							maintainli[mainidx].service_type2='维修';
						}else{
							maintainli[mainidx].service_type2='保养'
						}
						if(maintainli[mainidx].approve_status==1){
							maintainli[mainidx].state2='已申请';
						}else if(maintainli[mainidx].approve_status==2){
							maintainli[mainidx].state2='待审批'
						}else if(maintainli[mainidx].approve_status==3){
							maintainli[mainidx].state2='审批通过'
						}else if(maintainli[mainidx].approve_status==4){
							maintainli[mainidx].state2='审批不通过'
						}else{
							maintainli[mainidx].state2='已申请'
						}
						console.log(maintainli[mainidx].service_type2)
					}
					$('.maintain tbody').html(tbodymainf2(maintainli, 1, maintaintddata, maintainaddoperation, 'maintenance_record_id'))
					$('.maintain .pagination').html(pagechang1(res.count, 10, maintainpage))
					$('.maintain tbody tr').each(function(){
						for(var maidx=0;maidx<maintainli.length;maidx++){
							if($(this).data('id')==maintainli[maidx].maintenance_record_id){
								if(maintainli[maidx].approve_status){
									if(maintainli[maidx].approve_status==1){
										$(this).find('.tpl-table-black-operation').append(`
										<a class="request_check" href="javascript:;">
												<i class="am-icon-pencil"></i>请求审批
											</a>
											
										`)
									}else if(maintainli[maidx].approve_status==2){
										$(this).find('.tpl-table-black-operation').append(`
											<a class="service_check" href="javascript:;">
												<i class="am-icon-pencil"></i>审批
											</a>
										`)
									}else if(maintainli[maidx].approve_status==3){
										$(this).find('.tpl-table-black-operation').append(`
											<a class="service_check" href="javascript:;">
												<i class="am-icon-pencil"></i>详情
											</a>
										`)
									}else if(maintainli[maidx].approve_status==4){
										$(this).find('.tpl-table-black-operation').append(`
											<a class="service_check" href="javascript:;">
												<i class="am-icon-pencil"></i>详情
											</a>
										`)
									}
									
								}else{
									$(this).find('.tpl-table-black-operation').append(`
										<a class="request_check" href="javascript:;">
											<i class="am-icon-pencil"></i>请求审批
										</a>
										
									`)
								}
							}
						}
						
					})
					$('.request_check').on('click',function(){
						window.location.href='{:url("/unit/service/requestcheck")}?maintenance_record_id='+$(this).closest('tr').data("id")
//						layer.open({
//							type: 1,
//							title:'维修审批',
//							skin: 'layui-layer-rim', //加上边框
//							area: ['450px', '400px'], //宽高
//							content: `<div class='am-u-sm-12' style='font-size:14px'>
//										<form class="am-form-inline tpl-form-border-form" onkeydown="return(event.keyCode!=13)">
//										<div class='am-u-sm-12'>
//											<label for="user-name" class="am-u-sm-4 am-form-label cl-p am-text-truncate" style="line-height: 35px;">审批单位： </label>
//											<div class="am-form-group am-u-sm-8 cl-p">
//												<div class="am-u-sm-12 cl-p">
//													<input type="number" class="am-form-field tpl-form-no-bg" name="approveOrgn" value="{$item.use_time}">
//												</div>
//											</div>
//										</div>
//										<div class='am-u-sm-12'>
//											<label for="user-name" class="am-u-sm-4 am-form-label cl-p am-text-truncate" style="line-height: 35px;">审批人： </label>
//											<div class="am-form-group am-u-sm-8 cl-p">
//												<div class="am-u-sm-12 cl-p">
//													<input type="number" class="am-form-field tpl-form-no-bg" name="approvePerson" value="{$item.use_time}">
//												</div>
//											</div>
//										</div>
//										<div class='am-u-sm-12'>
//											<label for="user-name" class="am-u-sm-4 am-form-label cl-p am-text-truncate" style="line-height: 35px;">审批人： </label>
//											<div class="am-form-group am-u-sm-8 cl-p">
//												<div class="am-u-sm-12 cl-p">
//													<textarea name='approveDesc' style='height:200px' placeholder="请输入审批说明"></textarea>
//												</div>
//											</div>
//											
//										</div>
//										<div class='am-u-sm-12'>
//											<button class='am-btn am-btn-sm am-btn-primary'>审批</button>
//										</div>
//										</form>
//									</div>`
//						});
						
					});
					$('.service_check').on('click',function(){
						window.location.href='{:url("/unit/service/check")}?maintenance_record_id='+$(this).closest('tr').data("id")
					})
					$('.maintain .delete-list').on('click', function() {
						var mainid=$(this).closest('tr').data('id')
						layer.open({
							type: 1,
							title: false,
							id: "delete",
							content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否删除</div>",
							btn: ['确定', '取消'],
							btnAlign: 'c',
							shade: 0 ,
							yes: function() {
								Load()
								$.ajax({
									type:'post',
									url:"{:url('/unit/service/delete')}",
									data:{
										id:mainid
									},
									success:function(){
										Load(1)
										if(res.code==1){
											Msg('删除成功')
											layer.closeAll();
											var data={
												number:$('.maintain').find('input[name="number"]').val(),
												start_date:$('.maintain').find('input[name="start_date"]').val(),
												end_date:$('.maintain').find('input[name="end_date"]').val()
											};
											maintain(data)
										}else{
											Err(res.msg)
										}
									},
									error:function(){
										Err('请求失败')
									}
								})
						
			
							},
							btn2: function() {
								layer.closeAll();
							}
						});
					})
					
					$('.maintain .laquo').on('click', function() {
						maintainpage -= 1
						maintain(maintainpage)
					})
					$('.maintain .pageli').on('click', function() {
						maintainpage = $(this).html()
						maintain(maintainpage)
					})
					$('.maintain .raquo').on('click', function() {
						maintainpage += 1
						maintain(maintainpage)
					})
					$('.maintain .exit').on('click',function(){
						window.location.href='{:url("service/edit")}?maintenance_record_id='+$(this).closest('tr').data('id')
					})
					
				} else {
					Err(res.msg)
				}
			},
			error: function() {
				Load(1)
				$("#loading-first").fadeOut(500);
				Err('请求失败')
			}
		});
	}

	function refuel(data) {
		Load()
		if(data){
		data.page=refuelpage
		}
		$.ajax({
			type: "post",
			url: "{:url('/unit/refuel/datas')}",
			data: data,
			success: function(res) {
				Load(1)
				if(res.code == 1) {
					console.log(res)
					$("#loading-first").fadeOut(500);
					var refuelli = res.data.list
					var myScroll = new IScroll('#unit_main', {
						mouseWheel: true,
						disableMouse: true,
						disablePointer: true,
						scrollbars: 'custom',
						interactiveScrollbars: true,
					});

					$('.refuel tbody').html(tbodymainf2(refuelli, 1, refueltddata, refueladdoperation, 'refuel_record_id'))
					$('.refuel .pagination').html(pagechang1(res.data.count, 10, refuelpage))
					$('.refuel .delete-list').on('click', function() {
						var refuelid=$(this).closest('tr').data('id')
						layer.open({
							type: 1,
							title: false,
							id: "delete",
							content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否删除</div>",
							btn: ['确定', '取消'],
							btnAlign: 'c',
							shade: 0 ,
							yes: function() {
								Load()
								$.ajax({
									type:'post',
									url:"{:url('/unit/refuel/delete')}",
									data:{
										id:refuelid
									},
									success:function(){
										Load(1)
										if(res.code==1){
											Msg('删除成功')
											layer.closeAll();
											var data={
												number:$('.refuel').find('input[name="number"]').val(),
												start_date:$('.refuel').find('input[name="start_date"]').val(),
												end_date:$('.refuel').find('input[name="end_date"]').val()
											};
											refuel(data)
										}else{
											Err(res.msg)
										}
									},
									error:function(){
										Err('请求失败')
									}
								})
						
			
							},
							btn2: function() {
								layer.closeAll();
							}
						});
					})
					$('.refuel .laquo').on('click', function() {
						refuelpage -= 1
						refuel(refuelpage)
					})
					$('.refuel .pageli').on('click', function() {
						refuelpage = $(this).html()
						refuel(refuelpage)
					})
					$('.refuel .raquo').on('click', function() {
						refuelpage += 1
						refuel(refuelpage)
					})
					$('.refuel .exit').on('click',function(){
						window.location.href='{:url("refuel/edit")}?refuel_record_id='+$(this).closest('tr').data('id')
					})
					
				} else {
					Err(res.msg)
				}
			},
			error: function() {
				Load(1)
				$("#loading-first").fadeOut(500);
				Err('请求失败')
			}
		});
	}

	function survey(data) {
		Load()
		if(data){
		data.page=surveypage
		}
		$.ajax({
			type: "post",
			url: "{:url('/unit/survey/datas')}",
			data: data,
			success: function(res) {
				Load(1)

				console.log(res)
				if(res.code == 1) {
					$("#loading-first").fadeOut(500);
					var surveyli = res.data.list

					$('.survey tbody').html(tbodymainf2(surveyli, 1, surveytddata, surveyaddoperation, 'mot_record_id'))
					$('.survey .pagination').html(pagechang1(res.data.count, 10, surveypage))
					$('.survey .delete-list').on('click', function() {
						var surveyid=$(this).closest('tr').data('id')
						layer.open({
							type: 1,
							title: false,
							id: "delete",
							content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否删除</div>",
							btn: ['确定', '取消'],
							btnAlign: 'c',
							shade: 0 ,
							yes: function() {
								Load()
								$.ajax({
									type:'post',
									url:"{:url('/unit/survey/delete')}",
									data:{
										id:surveyid
									},
									success:function(){
										Load(1)
										if(res.code==1){
											Msg('删除成功')
											layer.closeAll();
											var data={
												number:$('.survey').find('input[name="number"]').val(),
												start_date:$('.survey').find('input[name="start_date"]').val(),
												end_date:$('.survey').find('input[name="end_date"]').val()
											};
											survey(data)
										}else{
											Err(res.msg)
										}
									},
									error:function(){
										Err('请求失败')
									}
								})
						
			
							},
							btn2: function() {
								layer.closeAll();
							}
						});
					})
					$('.survey .laquo').on('click', function() {
						surveypage -= 1
						survey(surveypage)
					})
					$('.survey .pageli').on('click', function() {
						surveypage = $(this).html()
						survey(surveypage)
					})
					$('.survey .raquo').on('click', function() {
						surveypage += 1
						survey(surveypage)
					})
					$('.survey .exit').on('click',function(){
						window.location.href='{:url("survey/edit")}?mot_record_id='+$(this).closest('tr').data('id')
					})
					
				} else {
					Err(res.msg)
				}
			},
			error: function() {
				Load(1)
				$("#loading-first").fadeOut(500);
				Err('请求失败')
			}
		});
	}

	function insure(data) {
		
		Load()
		if(data){
			data.page=insurepage
		}
		$.ajax({
			type: "post",
			url: "{:url('/unit/insure/datas')}",
			data: data,
			success: function(res) {
				Load(1)
				console.log(res);
				if(res.code == 1) {
				$("#loading-first").fadeOut(500);
				let policy_types = {
					1: '交通强制险',
					2: '商业险'
				};
				res.data.list.forEach(function(item){
					item.policy_type = policy_types[item.policy_type] ? policy_types[item.policy_type] : '';
				})
				var insureli = res.data.list

				$('.insure tbody').html(tbodymainf2(insureli, 1, insuretddata, insureaddoperation, 'car_insure_id'))
				$('.insure .pagination').html(pagechang1(res.data.count, 10, insurepage))
				$('.insure .delete-list').on('click', function() {
						var insureid=$(this).closest('tr').data('id')
						layer.open({
							type: 1,
							title: false,
							id: "delete",
							content: "<div class='am-vertical-align-middle' style='width:100%;text-align:center'>是否删除</div>",
							btn: ['确定', '取消'],
							btnAlign: 'c',
							shade: 0 ,
							yes: function() {
								Load()
								$.ajax({
									type:'post',
									url:"{:url('/unit/insure/delete')}",
									data:{
										id:insureid
									},
									success:function(){
										Load(1)
										if(res.code==1){
											Msg('删除成功')
											layer.closeAll();
											var data={
												number:$('.insure').find('input[name="number"]').val(),
												start_date:$('.insure').find('input[name="start_date"]').val(),
												end_date:$('.insure').find('input[name="end_date"]').val()
											};
											insure(data)
										}else{
											Err(res.msg)
										}
									},
									error:function(){
										Err('请求失败')
									}
								})
						
			
							},
							btn2: function() {
								layer.closeAll();
							}
						});
					})
				$('.insure .laquo').on('click', function() {
					insurepage -= 1
					insure(insurepage)
				})
				$('.insure .pageli').on('click', function() {
					insurepage = $(this).html()
					insure(insurepage)
				})
				$('.insure .raquo').on('click', function() {
					insurepage += 1
					insure(insurepage)
				})
				$('.insure .exit').on('click',function(){
					window.location.href='{:url("insure/edit")}?car_insure_id='+$(this).closest('tr').data('id')
				})
				
				} else {
					Err(res.msg)
				}
			},
			error: function() {
				$("#loading-first").fadeOut(500);
				Err('请求失败')
			}
		});
	}
})